package com.example.xxcjgl.ui.components

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import com.example.xxcjgl.data.models.Student
import com.example.xxcjgl.viewmodel.StudentViewModel

@Composable
fun StudentInfoCard(student: Student, onShowKeyDialog: () -> Unit) {
    Card(
        modifier = Modifier
            .fillMaxWidth()
            .padding(16.dp)
    ) {
        Column(modifier = Modifier.padding(16.dp)) {
            Text("学生姓名: ${student.studentName}")

            if (student.encryptionFlag == 1) {
                // 加密数据
                Text("数学成绩: *****")
                Text("语文成绩: *****")
                Button(onClick = onShowKeyDialog) {
                    Text("输入密钥查看")
                }
            } else {
                // 未加密数据
                Text("数学成绩: ${student.mathScore ?: "无"}")
                Text("语文成绩: ${student.chineseScore ?: "无"}")
            }
        }
    }
}

@Composable
fun MainScreen(
    onAddStudent: () -> Unit,
    onAddScore: () -> Unit,
    viewModel: StudentViewModel = hiltViewModel()
) {
    val currentStudent by viewModel.currentStudent.collectAsState<Student?>()
    var searchKeyword by remember { mutableStateOf("") }
    var showKeyDialog by remember { mutableStateOf(false) }
    var encryptionKey by remember { mutableStateOf("") }

    Scaffold(
        topBar = {
            TopAppBar(
                title = { Text("学习成绩查询") },
                actions = {
                    IconButton(onClick = { onAddStudent() }) {
                        Icon(Icons.Default.MoreVert, contentDescription = "添加学生")
                    }
                }
            )
        },
        floatingActionButton = {
            FloatingActionButton(onClick = { onAddScore() }) {
                Icon(Icons.Default.Add, contentDescription = "添加成绩")
            }
        }
    ) { padding ->
        Column(modifier = Modifier.padding(padding)) {
            // 搜索框
            OutlinedTextField(
                value = searchKeyword,
                onValueChange = { newValue: String ->
                    searchKeyword = newValue
                    if (newValue.isNotEmpty()) {
                        viewModel.searchStudents(newValue)
                    }
                },
                label = { Text("输入学生姓名拼音首字母") },
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(16.dp)
            )

            // 学生信息显示
            if (currentStudent != null) {
                StudentInfoCard(
                    student = currentStudent!!,
                    onShowKeyDialog = { showKeyDialog = true }
                )
            }

            // 密钥输入对话框
            if (showKeyDialog) {
                AlertDialog(
                    onDismissRequest = { showKeyDialog = false },
                    title = { Text("输入加密密钥") },
                    text = {
                        OutlinedTextField(
                            value = encryptionKey,
                            onValueChange = { newValue: String ->
                                encryptionKey = newValue
                            },
                            label = { Text("密钥") },
                            modifier = Modifier.fillMaxWidth()
                        )
                    },
                    confirmButton = {
                        Button(onClick = {
                            currentStudent?.let { student: Student ->
                                if (viewModel.verifyEncryptionKey(student.studentId, encryptionKey)) {
                                    // 显示解密后的数据
                                    showKeyDialog = false
                                }
                            }
                        }) {
                            Text("确认")
                        }
                    },
                    dismissButton = {
                        TextButton(onClick = { showKeyDialog = false }) {
                            Text("取消")
                        }
                    }
                )
            }
        }
    }
}